<html>
<head><meta charset="utf-8"><title>Is it worth the unsafe in `SocketAddrV4::ip` ? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Is.20it.20worth.20the.20unsafe.20in.20.60SocketAddrV4.3A.3Aip.60.20.3F.html">Is it worth the unsafe in `SocketAddrV4::ip` ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205785831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Is%20it%20worth%20the%20unsafe%20in%20%60SocketAddrV4%3A%3Aip%60%20%3F/near/205785831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Is.20it.20worth.20the.20unsafe.20in.20.60SocketAddrV4.3A.3Aip.60.20.3F.html#205785831">(Aug 03 2020 at 12:06)</a>:</h4>
<p>Ipv4Addr is a Copy type. There is no need to return the reference of Ipv4Addr.<br>
Especially when SockAddv4 doesn't contain Ipv4Addr.<br>
Now we left with unsafe cast when I change the underlined type of Ipv4Addr.</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">ip</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">Ipv4Addr</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">inner</span><span class="p">.</span><span class="n">sin_addr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">c</span>::<span class="n">in_addr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">Ipv4Addr</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Now when I change the underlined type of Ipv4Addr by a wrapper struct around u32.<br>
I don't know if the code above is safe and correct.<br>
How do I resolve this?</p>



<a name="205807783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Is%20it%20worth%20the%20unsafe%20in%20%60SocketAddrV4%3A%3Aip%60%20%3F/near/205807783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Is.20it.20worth.20the.20unsafe.20in.20.60SocketAddrV4.3A.3Aip.60.20.3F.html#205807783">(Aug 03 2020 at 15:25)</a>:</h4>
<p>The wrapper struct I talked about is in <a href="https://github.com/rust-lang/rust/issues/75110">#75110</a></p>



<a name="206173775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Is%20it%20worth%20the%20unsafe%20in%20%60SocketAddrV4%3A%3Aip%60%20%3F/near/206173775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Is.20it.20worth.20the.20unsafe.20in.20.60SocketAddrV4.3A.3Aip.60.20.3F.html#206173775">(Aug 06 2020 at 17:45)</a>:</h4>
<p>(we since talked about the cast in <a href="https://github.com/rust-lang/rust/issues/75110">#75110</a>, it should be fine now)</p>



<a name="206173802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Is%20it%20worth%20the%20unsafe%20in%20%60SocketAddrV4%3A%3Aip%60%20%3F/near/206173802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Is.20it.20worth.20the.20unsafe.20in.20.60SocketAddrV4.3A.3Aip.60.20.3F.html#206173802">(Aug 06 2020 at 17:45)</a>:</h4>
<p>and it is too late to discuss whether this should really return a reference, that type is stable now...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>